package com.mycompany.crm.controller;

import com.mycompany.crm.base.BaseController;
import com.mycompany.crm.service.PermissionService;
import com.mycompany.crm.service.UserService;
import com.mycompany.crm.utils.LoginUserUtil;
import com.mycompany.crm.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author jy
 * @version 1.0
 * @date 2022/1/4 14:42
 */
@Controller
public class IndexController extends BaseController {

    @Resource
    private UserService userService;

    @Resource
    private PermissionService permissionService;

    /**
     * 系统登录页
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "index";
    }

    /**
     * 系统登录页
     * @return
     */
    @RequestMapping("/")
    public String toIndex(){
        return "index";
    }

    /**
     * 系统界面欢迎页
     * @return
     */
    @RequestMapping("welcome")
    public String welcome() {
        return "welcome";
    }

    /**
     * 后端管理主页面
     * 我们需要将登录过后用户对应的userId通过工具类进行解密
     * 然后携带它去数据库查询相应的用户对象，再将对象存入session域中，
     * 以便main页面可以使用EL表达式获取用户名以显示到右上角
     * @return
     */
    @RequestMapping("main")
    public String main(HttpServletRequest request) {
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        User user = userService.selectByPrimaryKey(userId);
        request.getSession().setAttribute("user",user);
        //通过当前登录用户Id查询当前登录用户拥有的资源列表(查询对应资源的授权码)
        List<String> permissions = permissionService.queryUserRoleHasPermissionByUserId(userId);
        //将集合设置到session作用域中
        request.getSession().setAttribute("permissions",permissions);
        return "main";
    }

}